package top.zackyoung.bbs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.zackyoung.bbs.entity.User;
import top.zackyoung.bbs.service.ForumService;
import top.zackyoung.bbs.service.UserPublicService;
import top.zackyoung.bbs.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: ZackYoung
 * @time: 2020/6/16
 */
@RestController
public class UserPartyController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserPublicService userPublicService;
    @Autowired
    private ForumService forumService;

    /**
     * 粉丝数
     *
     * @param username
     * @return
     */
    @RequestMapping("queryFaceCount")
    public String queryFaceCount(String username) {
        Integer faceNum = null;
        if (username != null) {
            faceNum = userService.queryFaceNum(null, username);
        }
        return "\"" + faceNum.toString() + "\"";
    }

    /**
     * 关注数
     *
     * @param username
     * @return
     */
    @RequestMapping("queryFocusCount")
    public String queryFocusCount(String username) {
        Integer focusNum = userPublicService.queryFocusNum(username);
        return "\"" + focusNum.toString() + "\"";
    }

    /**
     * 论坛查看是否收藏
     *
     * @param topicId
     * @return
     */
    @RequestMapping("user/control/keep/queryAlreadyKeep")
    public String queryKeepCount(Integer topicId, HttpServletRequest req) {
        String isAlreadyKeep = "false";
        User user = (User) req.getSession().getAttribute("userList");
        Integer result = userService.queryAlreadyKeep(user.getId(), 1, topicId);
        if (result != 0) {
            isAlreadyKeep = "true";
        }
        return isAlreadyKeep;
    }

    /**
     * 添加收藏
     *
     * @param topicId
     * @param req
     * @return
     */
    @RequestMapping("user/control/keep/addKeep")
    public Map addKeep(Integer topicId, HttpServletRequest req) {
        Map map = new HashMap();
        User user = (User) req.getSession().getAttribute("userList");
        Integer result = userService.insertKeep(user.getId(), 1, topicId);
        if (result != 0) {
            map.put("success", "true");

        } else {
            map.put("success", "false");
            map.put("error", "已经收藏了，不用再次添加");
        }
        return map;
    }

    /**
     * 查询论坛文章被收藏数
     */
    @RequestMapping("queryKeepCount")
    public Integer queryKeepCount(Integer topicId) {
        Integer result = userService.queryAlreadyKeep(null, 1, topicId);
        return result;
    }

    /**
     * 查询是否关注了该对象
     *
     * @param username
     * @param req
     * @return
     */
    @RequestMapping("user/control/focus/queryIsFocus")
    public String queryIsFocus(String username, HttpServletRequest req) {
        String m = null;
        User user = (User) req.getSession().getAttribute("userList");
        Integer result = userService.queryIsFocus(user.getId(), "1", username);
        if (result > 0) {
            m = "true";
        }
        return m;
    }

    /**
     * 添加关注
     *
     * @param username
     * @param req
     * @return
     */
    @RequestMapping("user/control/focus/add")
    public Map addFocus(String username, HttpServletRequest req) {
        Map map = new HashMap();
        Map error = new HashMap();
        User user = (User) req.getSession().getAttribute("userList");
        map.put("success", "false");
        if (username.equals(user.getUsername())) {
            error.put("follow", "不能关注自身");
            map.put("error", error);
        } else {
            int result = userService.insetFocus(user.getId(), "1", username);
            if (result > 0) {
                map.put("success", "true");

            } else {
                map.put("success", "false");
                error.put("follow", "已经关注了");
                map.put("error", error);

            }
        }
        return map;
    }

    /**
     * 查询给文章是否点过赞
     *
     * @param topicId
     * @param req
     * @return
     */
    @RequestMapping("user/control/like/queryAlreadyLike")
    public String queryForumLikeCount(Integer topicId, HttpServletRequest req) {
        String m = null;
        User user = (User) req.getSession().getAttribute("userList");
        Integer result = userService.queryLikeCount(user.getId(), 1, topicId);
        if (result > 0) {
            m = "true";
        }
        return m;
    }

    /**
     * 文章点赞数
     *
     * @param topicId
     * @return
     */
    @RequestMapping("queryLikeCount")
    public Integer queryLikeCount(Integer topicId) {
        Integer result = userService.queryLikeCount(null, 1, topicId);
        return result;
    }

    /**
     * 点赞
     *
     * @param topicId
     * @param req
     * @return
     */
    @RequestMapping("user/control/like/add")
    public Map addLike(Integer topicId, HttpServletRequest req) {
        Map map = new HashMap();
        Map error = new HashMap();
        User user = (User) req.getSession().getAttribute("userList");
        int result = userService.insertLike(user.getId(), 1, topicId);

        map.put("success", "false");
        if (result > 0) {
            map.put("success", "true");
        } else {
            map.put("success", "false");
            error.put("follow", "已经点过赞了");
            map.put("error", error);
        }

        return map;
    }

    /**
     * 添加回复
     * @param commentId
     * @param content
     * @param req
     * @return
     */
    @RequestMapping("user/control/comment/addReply")
    public Map addReply(Integer commentId, String content, HttpServletRequest req) {
        Map map = new HashMap();
        Map error = new HashMap();
        User user = (User) req.getSession().getAttribute("userList");
        int result = forumService.addComment(user.getId(), 3, commentId, content);
        map.put("success", "false");
        if (result > 0) {
            map.put("success", "true");
        } else {
            map.put("success", "false");
            error.put("follow", "已经评论过了");
            map.put("error", error);
        }
        return map;
    }

    /**
     * 添加评论
     * @param topicId
     * @param content
     * @param req
     * @return
     */
    @RequestMapping("user/control/comment/add")
    public Map add(Integer topicId, String content, HttpServletRequest req) {
        Map map = new HashMap();
        Map error = new HashMap();
        User user = (User) req.getSession().getAttribute("userList");
        int result = forumService.addComment(user.getId(), 1, topicId, content);
        map.put("success", "false");
        if (result > 0) {
            map.put("success", "true");
        } else {
            map.put("success", "false");
            error.put("follow", "已经评论了");
            map.put("error", error);
        }
        return map;
    }

    /**
     * 删除关注
     * @param focusId
     * @return
     */
    @RequestMapping("user/control/focus/delete")
    public Map deleteFocus(Integer focusId) {
        int result = userService.deleteFocus(focusId);
        Map map =i(result,"已经被删除了");

        return map;
    }
    @RequestMapping("user/control/like/delete")
    public Map deleteLike(Integer likeId,HttpServletRequest req) {
        int result = userService.deleteLike(likeId,userList(req).getId());
        Map map =i(result,"已经被删除了");

        return map;
    }

    User userList(HttpServletRequest req){
        User user = (User) req.getSession().getAttribute("userList");
        return user;
    }
    public  Map i(int t,String errorString){
        Map map = new HashMap();
        Map error = new HashMap();
        int result = t;
        map.put("success", "false");
        if (result > 0) {
            map.put("success", "true");
        } else {
            map.put("success", "false");
            error.put("follow", errorString);
            map.put("error", error);
        }
        return map;
    }


}
